

//
//  WBStatusViewModel.swift
//  WeiBoAPP
//
//  Created by 张学飞 on 2019/1/16.
//  Copyright © 2019 zxf. All rights reserved.
//

import Foundation
import YYModel

/**
 如果累需要使用 kvc 或者字典转模型框架设置对象值，累就需要继承自 NSObject
 如果累只是包装一些代码逻辑，可以不用任何父类，好处：更加轻量级，
 */
class WBStatusViewModel {
    
    lazy var statusList = [WBStatueViewModel]()
    
    func loadStatus(pullUp:Bool, completion:@escaping (_ isSuccess:Bool) -> ()) {
        
        var array = [WBStatueViewModel]()
//            for i in 0..<10 {
        for i  in 0..<10 {
            let model = WBStatueViewModel(model: WBStatusModel())
            model.status.text = "statusList \(i) " + (pullUp ? "上拉" : "下拉")
            array.append(model)
        }
        
        if pullUp {
            self.statusList = array + self.statusList
        }else{
            self.statusList += array
        }
        
        
        completion(true)
        
        
        // ******  网络接口
        WBNetworkManager.shared.statusList { (list, isSuccess) in

//            guard  (NSArray.yy_modelArray(with: WBStatusModel.self , json: list ?? [])  as? [WBStatusModel]) != nil else {
//            let newArray = NSArray.yy_modelArray(with: WBStatusModel.self, json: list ?? [])
                                                 
//            guard let newArray = NSArray.yy_modelArray(with: WBStatusModel.self, json: list ?? []) else {
//                return
//            }
            
//            guard  let array = NSArray.yy_modelArray(with: WBStatusModel.self , json: list ?? [])  as? [WBStatusModel] else {
//                completion(isSuccess)
//                return
//            }

            //1.遍历数组
            var modelArray = [WBStatueViewModel]();

            for dic in list ?? [:]  {
//                let status  = WBStatusModel()
                let status = WBStatusModel.yy_model(withJSON: dic)
//                status.yy_modelSet(with: dic as! [AnyHashable : Any])
                
                guard let p = status else {
                    return
                }
                let viewModel = WBStatueViewModel(model:p)

                modelArray.append(viewModel)

            }


            completion(isSuccess)
        }
    }
    
    
   
    
}
